<template>
	<view class="container" >
		<!-- 导航栏 -->
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
			titleStyle="{height: '52px'}" :title="sample.name" @clickLeft="back">
			<block slot="right">
				<view class="city">
					<text  style="font-size: 15px;" @click="confirm">确认领取</text>
				</view>
			</block>
		</uni-nav-bar>
		
		<uni-section title="样品领取" type="line">
			<view class="example">
				<uni-forms ref="baseForm" :modelValue="sample" labelPosition="top" :rules="rules">
					<uni-forms-item label="领样部门" name="department" labelPosition="top">
						<uni-combox :candidates="orgNames" v-on:input="orgChanged" placeholder="请选择部门"
							v-model="sample.consumeDepName" ></uni-combox>
					</uni-forms-item>
					<uni-forms-item label="领样人" name="consumer" labelPosition="top">
						<uni-combox :candidates="userNames" v-on:input="userChanged" placeholder="请选择领样人"
							v-model="sample.consumerName" ></uni-combox>
					</uni-forms-item>
				</uni-forms>
			</view>
		</uni-section>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sample:{
					consumeDepName: "",
					consumerName: "",
					
				},
				enableRecieveBtn:false,
				showConfirmBtn:false,
				showConfirmModal:false,
				selectedOrgName: '',
				selectedUserName: '',
				orgNames: [],
				orgs: [],
				userNames: [],
				users: [],
				rules: {
					department: {
						rules: [{
							required: true,
							errorMessage: '请选择领样部门',
						}]
					},
					consumer: {
						rules: [{
							required: true,
							errorMessage: '请选择领养人',
						}]
					}
				}
			}
		},
		onLoad(option) {
			// 加载样品存放地点(字典数据，Location)
			this.loadSampleInfo(option.id);
			this.queryOrgList();
		},
		computed: {},
		methods: {
			confirm(){
				var this_ = this; //存储this
				this_.$refs.baseForm.validate().then(res => {
					this_.$u.api.forwardSample(this_.sample).then(res=>{
						this_.$u.toast('领取成功');
						this_.back();
					});
				});
			},
			loadSampleInfo(sampleId){
				var this_ = this;
				this.$u.api.querySampleInfo(sampleId).then(res=>{
					this_.sample = res.data;
					if(this_.sample.consumeDep)
						this_.queryUserList({'deptId':this_.sample.consumeDep});
				});
			},
			userChanged(userName) {
				var this_ = this; //存储this
				//如果填入的用户名称非法，则主动清空内容
				var userId = this_.resolveUserIdWithSelectedUserName(userName);

				if (!userId) {
					this_.sample.consumerName='';
				} else {
					this_.sample.consumer = userId;
				}
			},
			orgChanged(orgName) {
				if (orgName === '') return;
				//如果填入的机构名称非法，则主动清空内容
				var orgId = this.resolveDepIdWithSelectedOrgName(orgName);
				if (!orgId) {
					this.sample.consumeDepName = '';
					//机构切换,则领用人选择项清空,领用人选项刷新
				} else {
					this.sample.consumerName = '';
					this.sample.consume = '';
					this.sample.consumeDep = orgId;
					var params = {
						'deptId': orgId
					};
					this.queryUserList({
						'params': params
					});
				}

			},
			resolveDepIdWithSelectedOrgName(orgName) {
				for (let index = 0; index < this.orgs.length; index++) {
					if (this.orgs[index].deptName === orgName) return this.orgs[index].deptId;
				}
				return null;
			},
			resolveUserIdWithSelectedUserName(userName) {
				for (let index = 0; index < this.users.length; index++) {
					if (this.users[index].userName === userName) return this.users[index].userId;
				}
				return null;
			},
			queryUserList(params) {
				var this_ = this; //存储this

				this.$u.api.queryUserList(params).then((response) => {
					//这个控件只能支持字符串选取
					this_.users = response.rows;
					let userNames = [response.rows.length];
					let newUserNames = [response.rows.length];
					for (let index = 0; index < response.rows.length; index++) {
						userNames[index] = response.rows[index].nickName;
					}
					this.newUserNames = newUserNames;
					this_.userNames = userNames;
				});
			},
			queryOrgList() {
				var this_ = this; //存储this
				this.$u.api.queryOrgList().then((response) => {
					//这个控件只能支持字符串选取
					this_.orgs = response.data;
					let orgNames = [response.data.length];
					for (let index = 0; index < response.data.length; index++) {
						orgNames[index] = response.data[index].deptName;
					}
					this_.orgNames = orgNames;
				});
			},
			back() {
				this.backPageRefresh();
			}
		}
	}
</script>

<style>
	.uni-section {
		margin-top: 0px !important;
		background-color: #eee;
	}

	.custom-image-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.example {
		background-color: white;
		padding: 10px 20px 15px 20px;
	}

	.segmented-control {
		margin-bottom: 10px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>
